<?php

namespace common\models\datas;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%message_log}}".
 *
 * @property integer $id
 * @property integer $site_id
 * @property integer $sender_id
 * @property string $receiver_arr
 * @property string $content
 * @property integer $created_at
 * @property string $send_way
 * @property integer $status
 * @property string $fail_arr_sys
 * @property string $fail_arr_mail
 * @property string $fail_arr_wechat
 * @property string $fail_arr_sms
 * @property integer $admin_id
 * @property integer $type_id
 * @property integer $is_all
 */
class MsgLog extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%msg_log}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [//开发完后考虑一下群发
            [['site_id', 'message_type_id', 'created_at','admin_id','state'], 'integer'],
            [['receiver_info', 'error_info', 'message_name', 'message_text', 'send_way','send_group'], 'string'],
            [['send_way','receiver_info'], 'required'],
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
//        $fields['type_id'] = function () {
//            switch ($this->type_id) {
//                case 9:
//                    return \Yii::t('frontend', '活动消息');
//                case 8:
//                    return \Yii::t('frontend', '公告消息');
//                case 7:
//                    return \Yii::t('frontend', '课程消息');
//                case 6:
//                    return \Yii::t('frontend', '积分消息');
//                case 5:
//                    return \Yii::t('frontend', '个人资料');
//                case 4:
//                    return \Yii::t('frontend', '账户信息');
//                case 3:
//                    return \Yii::t('frontend', '点评');
//                case 2:
//                    return \Yii::t('frontend', '成长日志');
//                case 1:
//                    return \Yii::t('frontend', '家委会消息');
//                case 10:
//                    return \Yii::t('frontend','处理结果');
//            }
//        };
        $fields['send_way'] = function () {
            $send = unserialize($this->send_way);
            $arr = [];
            foreach ($send as $v) {
                switch ($v) {
                    case 'sms':
                        $arr[$v] = \Yii::t('frontend', '短信');
                        continue;
                    case 'wechat':
                        $arr[$v] = \Yii::t('frontend', '微信');
                        continue;
                    case 'email':
                        $arr[$v] = \Yii::t('frontend', '邮件');
                        continue;
                    case 'sys':
                        $arr[$v] = \Yii::t('frontend', '系统');
                        continue;
                }
            }
            return implode('|',$arr);

        };
//        $fields['is_all'] = function () {
//            switch ($this->is_all) {
//                case 0:
//                    return \Yii::t('frontend', '否');
//                case 1:
//                    return \Yii::t('frontend', '是');
//            }
//        };
//        $fields['receiver_arr'] = function () {
//            $receiver_arr = unserialize($this->receiver_arr);
//            if(!is_array($receiver_arr))return null;
//            $text='';
//            foreach ($receiver_arr as $k=>$v){
//                empty($text)?$text.=$v:$text.="\r\n".'|'.$v;
//            }
//            return $text;
//        };
//        $fields['fail_arr_sms'] = function () {
//            $fail_arr_sms = unserialize($this->fail_arr_sms);
//            if(!is_array($fail_arr_sms))return null;
//            $text='';
//            foreach ($fail_arr_sms as $k=>$v){
//                empty($text)?$text.=$v:$text.="\r\n".'|'.$v;
//            }
//            return $text;
//        };
//        $fields['status'] = function () {
//            $status = unserialize($this->status);
//            return Util::array2text($status);
//        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'site_id' => Yii::t('common', '分店编号'),
            'message_type_id' => Yii::t('common', '消息类型'),
            'send_way' => Yii::t('common', '发送方式'),
            'receiver_info' => Yii::t('common', '接收者信息'),
            'message_name' => Yii::t('common', '消息名称'),
            'message_text' => Yii::t('common', '消息内容'),
            'send_group' => Yii::t('common', '接收对象类型'),
            'send_time' => Yii::t('common', '消息发送时间'),
            'created_at' => Yii::t('common', '创建时间'),
            'state' => Yii::t('common', '状态'),
            'error_info' => Yii::t('common', '失败信息')
        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return $this->admin->username;
            },
//            'frontend_operate' => function () {
//                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR,$this->attributes))->create();
//
//            },

        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
    }
}